package algorithm;

public class LinkedList_reverse_sj {

    public static void main(String[] args) {
        //null→ 1→ 2→ 3
        Node nodeHead = new Node(0);//head
        Node n1 = new Node(1);
        Node n2 = new Node(2);
        Node n3 = new Node(3);
        nodeHead.next = n1;
        n1.next = n2;
        n2.next = n3;
        Node currentNode = null;//当前节点
        //打印
        currentNode = nodeHead;
        while (currentNode != null) {
            System.out.print(currentNode.data + ", ");
            currentNode = currentNode.next;
        }
        System.out.println("");
        System.out.println("------ Hello World!");

        //翻转链表
        //3→ 2→ 1→ null
        //Node currentNode = null;//当前节点
        Node prevNode = null;//上一节点，previous
        Node nextNode = null;//下一节点
        //Node sentinelNode = new Node();//哨兵节点

        currentNode = n1;
        while (currentNode != null) {
            nextNode = currentNode.next;

            if (currentNode.equals(n1)) {
                currentNode.next = null;
            } else {
                currentNode.next = prevNode;
            }
            prevNode = currentNode;

            currentNode = nextNode;
        }
        nodeHead.next = prevNode;
        //打印
        currentNode = nodeHead;
        while (currentNode != null) {
            System.out.print(currentNode.data + ", ");
            currentNode = currentNode.next;
        }
    }
}
